package com.hongdee.scm.certificate.controller;

import com.alibaba.fastjson.JSON;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.certificate.entity.TCrmCarCertificate;
import com.hongdee.scm.certificate.service.ITCrmCarCertificateService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:TCrmCarCertificateController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.crm.certificate.controller
 * 作  者: 术洪超
 * 日  期:2017-06-13 15:22
 */
@RestController
@RequestMapping("/scm/certificate")
public class TCrmCarCertificateController {
    private static Logger log = Logger.getLogger(TCrmCarCertificateController.class);
    @Autowired
    private ITCrmCarCertificateService itCrmCarCertificateService;


    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加的实体内容
     * @author 术洪超
     */

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "taxcode", defaultValue = "") String taxcode,
                             @RequestParam(name = "orgcode", defaultValue = "") String orgcode,
                             @RequestBody Map data) throws Exception {

        String str = JSON.toJSONString(data);
       // JSON.parse(str,TCrmCarCertificate.class);
        TCrmCarCertificate obj =   JSON.parseObject(str,TCrmCarCertificate.class);
        log.info(str);
        System.out.println(str);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(false);

        if (null == data)
        {
            res.setMsg("参数不对，未获取对象数据！");
            return res;
        }
        if (StringUtils.isEmpty(obj.getVin()))
        {
            res.setMsg("参数不对，车架号为空！");
            return res;
        }
        if (StringUtils.isEmpty(obj.getCertificateNum()))
        {
            res.setMsg("参数不对，合格证号为空！");
            return res;
        }

        obj.setTaxCode(taxcode);
        obj.setCompanyId(orgcode);
        boolean rs = this.itCrmCarCertificateService.saveOrUpdate(obj, "");
        res.setSuccess(rs);
        if (!rs)
        {
            res.setMsg("保存失败，后台异常");
        }
        return res;
    }

}
